package ui.activity;

import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.fy.fyzf.R;
import com.fy.fyzf.base.BaseActivity;
import com.fy.fyzf.bean.SignContractSearchBean;
import com.fy.fyzf.bean.SignDetailBean;
import com.fy.fyzf.bean.SignContractListBean;
import com.fy.fyzf.presenter.SignContractPresenter;
import com.fy.fyzf.utils.MathUtils;
import com.fy.fyzf.utils.SPUtils;
import com.fy.fyzf.view.SignContractCallBack;
import com.fy.fyzf.view.SpacesItemDecoration;
import com.scwang.smart.refresh.layout.SmartRefreshLayout;
import com.scwang.smart.refresh.layout.api.RefreshLayout;
import com.scwang.smart.refresh.layout.listener.OnLoadMoreListener;
import com.scwang.smart.refresh.layout.listener.OnRefreshListener;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;
import io.reactivex.internal.operators.single.SingleNever;
import ui.adapter.SignContractAdapter;

/**
 * 签约房源
 */
public class SignContractActivity extends BaseActivity<SignContractPresenter> implements SignContractCallBack {

    @BindView(R.id.rightImage)
    ImageView rightImage;
    @BindView(R.id.recycleView)
    RecyclerView recycleView;
    @BindView(R.id.smartRefresh)
    SmartRefreshLayout smartRefresh;

    private int page = 1;
    private int tag = 1;
    private int pageSize = 10;
    private int customersId;
    private SignContractAdapter signContractAdapter;

    @Override
    protected SignContractPresenter createPresenter() {
        return new SignContractPresenter(this);
    }

    @Override
    protected int provideContentViewId() {
        return R.layout.activity_sign_contract;
    }

    @Override
    public void initView() {
        super.initView();
        setTitleName("签约");
        rightImage.setVisibility(View.VISIBLE);
        rightImage.setBackgroundResource(R.mipmap.add_sign);

        smartRefresh.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                tag = 1;
                page = 1;
                initData();

            }
        });

        smartRefresh.setOnLoadMoreListener(new OnLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                tag = 2;
                page++;
                initData();
            }
        });

        signContractAdapter = new SignContractAdapter();
        recycleView.setLayoutManager(new LinearLayoutManager(this));
        recycleView.addItemDecoration(new SpacesItemDecoration(20));
        recycleView.setAdapter(signContractAdapter);

        signContractAdapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                List<SignContractListBean.ListBean> data = adapter.getData();
                signType = 2;
                performanceId = Integer.parseInt(data.get(position).getPerformanceId());
                mPresenter.getSingnContractdetail(performanceId);
            }
        });
    }

    private Integer signType;
    private Integer performanceId;
    private SignContractSearchBean signContractSearchBean = new SignContractSearchBean();
    @Override
    public void initData() {
        super.initData();
        registerEventBus(this);
        customersId = getIntent().getIntExtra("customersId", 0);
        signContractSearchBean.setCustomerId(customersId);
        signContractSearchBean.setPageNum(page);
        signContractSearchBean.setPageSize(pageSize);

        mPresenter.getSignContractList1(signContractSearchBean);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void saveSuccess(String s) {
        if (s.equals("saveSuccess")) {
            initData();
        }
    }
    @Override
    protected void onDestroy() {
        super.onDestroy();
        unregisterEventBus(this);
    }

    @Override
    public void getSifnContractListSuccess(SignContractListBean signContractListBean) {
        smartRefresh.finishRefresh();
        smartRefresh.finishLoadMore();
        if (signContractListBean.getList().size() == 0 || null == signContractListBean) {
            View view = LayoutInflater.from(this).inflate(R.layout.layout_nodata_common, null);
            signContractAdapter.setEmptyView(view);
        }
        if (tag == 1) {
            signContractAdapter.setNewData(signContractListBean.getList());
        } else {
            signContractAdapter.addData(signContractListBean.getList());
        }
    }

    @Override
    public void getSingnContractdetail(SignDetailBean signContractDetailBean) {
        if (signContractDetailBean.getStep() == 4) {
            Intent intent = new Intent(this, SignContractDetailActivity.class);
            intent.putExtra("performanceId",performanceId);
            intent.putExtra("customersId", signContractDetailBean.getCustomerId());
            intent.putExtra("step", signContractDetailBean.getStep());
            startActivity(intent);
        } else {
            Intent intent = new Intent(this, SignStepActivity.class);
            intent.putExtra("performanceId",performanceId);
            intent.putExtra("customersId", customersId);
            intent.putExtra("step", signContractDetailBean.getStep());
            intent.putExtra("type", 2);
            startActivity(intent);
        }
        /*switch (signContractDetailBean.getStep()) {
            case 1:
                Intent intent = new Intent(this, AddSignActivityOne.class);
                intent.putExtra("type", signType);  //1 加号进入 2  列表进入
                intent.putExtra("customersId", customersId);
                intent.putExtra("performanceId",performanceId);
                startActivity(intent);
                break;
            case 2:
                 intent = new Intent(this, AddSignTwoActivity.class);
                intent.putExtra("type", signType);
                intent.putExtra("customersId", customersId);
                intent.putExtra("performanceId",performanceId);
                startActivity(intent);
                break;
            case 3:
                intent = new Intent(this, AddSignThreeActivity.class);
                intent.putExtra("type", signType);
                intent.putExtra("customersId", customersId);
                intent.putExtra("performanceId",performanceId);
                startActivity(intent);
                break;
            case 4: //进入详情页面
                if (signType == 1) {
                    SPUtils.getInstance(this).remove("performanceId");
                    intent = new Intent(this, AddSignActivityOne.class);
                    intent.putExtra("type", signType);  //1 加号进入 2  列表进入
                    intent.putExtra("customersId", customersId);
                    intent.putExtra("performanceId",performanceId);
                    startActivity(intent);
                }
                break;
        }*/

    }

    @OnClick(R.id.rightImage)
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rightImage:
                Intent intent ;
              /*  signType = 1;
                String performanceId = SPUtils.getInstance(this).getString("performanceId", "");

                if (null == performanceId || performanceId.equals("") || performanceId.equals("4")) {
                    intent = new Intent(this, AddSignActivityOne.class);
                    intent.putExtra("customersId", customersId);
                    startActivity(intent);
                } else {
                    mPresenter.getSingnContractdetail(Integer.parseInt(MathUtils.rvZeroAndDot(performanceId)));
                }*/
                intent = new Intent(this, SignStepActivity.class);
                intent.putExtra("customersId", customersId);
                intent.putExtra("type", 1);
//                intent.putExtra("performanceId", performanceId);
                startActivity(intent);
                break;
        }
    }
}